package com.waimai.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waimai.demo.Service.CategoryService;
import com.waimai.demo.Service.DishFlavorService;
import com.waimai.demo.Service.DishService;
import com.waimai.demo.back.R;
import com.waimai.demo.domain.Category;
import com.waimai.demo.domain.Dish;
import com.waimai.demo.domain.DishFlavor;
import com.waimai.demo.dto.DishDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@RequestMapping("/dish")
@RestController
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> sava(@RequestBody DishDto dishDto, HttpServletRequest req) {
        Long id = (Long) req.getSession().getAttribute("employee");
        System.out.println(dishDto.toString());
        dishService.savawithflavor(dishDto, id);
        return R.success("新增菜品成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        List<DishDto> dishDtos = new ArrayList<>();
        Page<Dish> iPage = new Page<>(page, pageSize);
        Page<DishDto> page1 = new Page<>();
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(name != null, Dish::getName, name);
        lambdaQueryWrapper.orderByDesc(Dish::getUpdateTime);

        dishService.page(iPage, lambdaQueryWrapper);
        BeanUtils.copyProperties(iPage, page1, "records");
        List<Dish> records = iPage.getRecords();
        for (Dish record : records) {
            Long categoryId = record.getCategoryId();
            Category category = categoryService.getById(categoryId);
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(record, dishDto);
            dishDto.setCategoryName(category.getName());
            dishDtos.add(dishDto);
        }
        page1.setRecords(dishDtos);
        return R.success(page1);

    }

    @GetMapping("/{id}")
    public R<DishDto> update(@PathVariable Long id) {

        DishDto dishDto = dishService.updatewithflavor(id);
        System.out.println("=========你妈的比" + dishDto.toString());
        return R.success(dishDto);
    }

    @PutMapping
    public R<String> update(DishDto dishDto) {
        dishService.removewithflavor(dishDto);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    @Cacheable
    public R<List<DishDto>> huixian(Dish dish) {
        log.info("===================" + dish.getCategoryId());
        Long id = dish.getCategoryId();
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId, id);
        List<Dish> list = dishService.list(lambdaQueryWrapper);
        List<DishDto> dishDto = new LinkedList();
        for (Dish dish1 : list) {
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(DishFlavor::getDishId, dish1.getId());
            List<DishFlavor> dishFlavors = dishFlavorService.list(lambdaQueryWrapper1);
            DishDto dishDto1 = new DishDto();
            BeanUtils.copyProperties(dish1, dishDto1);
            dishDto1.setFlavors(dishFlavors);
            dishDto.add(dishDto1);
        }
        log.info("===================我真的好难啊" + dishDto);
        return R.success(dishDto);
    }

    @PostMapping("/status/0")
    public R<String> jinyong(Long ids) {
        Dish dish = dishService.getById(ids);
        dish.setStatus(0);
        dishService.updateById(dish);
        return R.success("修改成功");
    }

    @PostMapping("/status/1")
    public R<String> kaiqi(Long ids) {
        Dish dish = dishService.getById(ids);
        dish.setStatus(1);
        dishService.updateById(dish);
        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> delete(Long ids) {
        Dish dish = dishService.getById(ids);
        dishService.removeById(dish);
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, ids);
        dishFlavorService.remove(lambdaQueryWrapper);
        return R.success("删除成功");
    }
}



